package main

import (
	"fmt"
	"math"
	"sort"
	"time"
)

//定义求面积的接口
type AreaInterface interface {
	Area() float64
}

//定义多态函数，传入不同图形的面积接口，得到不同的图形面积
func RequestArea(a AreaInterface) float64 {
	return a.Area()
}

//定义圆形
type Circle struct {
	r float64
}

//定义圆形构造函数
func NewCircle(r float64) *Circle {
	return &Circle{r}
}

//给圆形定义面积方法
func (c *Circle) Area() float64 {

	return math.Pi * c.r * c.r
}

type Triangle struct {
	a int
	h int
}

func NewTriangle(a, h int) *Triangle {
	return &Triangle{a, h}
}

func (t *Triangle) Area() float64 {
	return float64(0.5 * float64(t.a) * float64(t.h))
}

type Rectangle struct {
	a int
	b int
}

func NewRectangle(a, b int) *Rectangle {
	return &Rectangle{a, b}
}
func (r *Rectangle) Area() float64 {
	return float64(r.a * r.b)
}

type area1 struct {
	key   string
	value float64
}

func main() {
	// 第一题
	//初始化圆形
	cir := NewCircle(2.5)
	//打印圆形参数
	fmt.Printf("%T %[1]v\n", cir)
	//求圆形面积
	cirArea := RequestArea(cir)
	//打印圆形面积
	fmt.Println("cirArea:", cirArea)

	tri := NewTriangle(2, 3)
	fmt.Printf("%T %[1]v\n", tri)
	triArea := RequestArea(tri)
	fmt.Println("triArea:", triArea)

	rec := NewRectangle(2, 2)
	fmt.Printf("%T %[1]v\n", rec)
	recArea := RequestArea(rec)
	fmt.Println("recArea:", recArea)

	//构造一个结构体   第二题
	areas := make([]area1, 3)
	areas[0] = area1{"cirArea", cirArea}
	areas[1] = area1{"triArea:", triArea}
	areas[2] = area1{"recArea:", recArea}
	fmt.Println(areas)

	sort.Slice(areas, func(i, j int) bool {
		return areas[i].value > areas[j].value
	})

	fmt.Println(areas)

	//第三题已经在上一个作业完成了

	//第四题
	tz, _ := time.LoadLocation("Asia/Shanghai")
	s1 := "2015/11/06 09:30:00"
	layout := "2006/01/02 15:04:05"
	t1, _ := time.ParseInLocation(layout, s1, tz)
	//毫秒数
	fmt.Println(t1.UnixMilli())
	//格式化输出
	fmt.Println(t1)
	//周几,当年第几周
	fmt.Println(t1.Weekday())
	fmt.Println(t1.ISOWeek())

	//查距今多少天
	var t = time.Now()
	t_now := t.Format("2006/01/02 15:04:05 +0800")
	fmt.Println(t_now)
	detla := t.Sub(t1)
	// fmt.Printf("%T %[1]v", detla)
	fmt.Printf("距现在%v 天", int(detla.Hours()/24))

}
